package cz.muni.fi.pa165.musiclibrary.musiclibraryweb;

import org.apache.wicket.ConverterLocator;
import org.apache.wicket.IConverterLocator;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import org.apache.wicket.settings.IExceptionSettings;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.joda.time.Duration;
import org.joda.time.LocalDate;

import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.album.AlbumPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.artist.ArtistPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.error.ErrorPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.genre.GenrePage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.search.SearchPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.search.result.SearchResultPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.song.SongPage;

/**
 * Web application configuration and initialization.
 * 
 * @author Maros Kucbel
 * @date 2012-11-16T19:30:09+0100
 */
public final class MusicLibraryApplication extends WebApplication {

    @Override
    public Class<? extends WebPage> getHomePage() {
        return SearchPage.class;
    }

    @Override
    protected void init() {
        super.init();
        this.getComponentInstantiationListeners().add(new SpringComponentInjector(this));
        this.getApplicationSettings().setInternalErrorPage(ErrorPage.class);
        this.getExceptionSettings().setUnexpectedExceptionDisplay(IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE);
        this.mount(new CleanUrlMapper("/", SearchPage.class));
        this.mount(new CleanUrlMapper("/search", SearchResultPage.class));
        this.mount(new CleanUrlMapper("/song", SongPage.class));
        this.mount(new CleanUrlMapper("/artist", ArtistPage.class));
        this.mount(new CleanUrlMapper("/album", AlbumPage.class));
        this.mount(new CleanUrlMapper("/genre", GenrePage.class));
        this.mount(new CleanUrlMapper("/error", ErrorPage.class));
    }

    @Override
    protected IConverterLocator newConverterLocator() {
        ConverterLocator locator = (ConverterLocator) super.newConverterLocator();
        locator.set(LocalDate.class, new LocalDateConverter());
        locator.set(Duration.class, new DurationConverter());
        return locator;
    }

    @Override
    public Session newSession(Request request, Response response) {
        return new MusicLibrarySession(request);
    }
    
}